#include "main.h"
unsigned char num = 0;
unsigned char Key_Change = 0;
//unsigned char i = 1;

int isLeapYear(int year)
{
    // 判断是否为闰年
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    {
        return 1; // 是闰年
    }
    else
    {
        return 0; // 不是闰年
    }
}

void Time_Logic_Add(Time *time)
{
    if (time->Second > 59)
    {
        time->Second = 0;
        time->Minute++;
    }
    if (time->Minute > 59)
    {
        time->Minute = 0;
        time->Hour++;
    }
    if (time->Hour > 23)
    {
        time->Hour = 0;
        time->Day++;
        //time->Weekday++;
    }
    switch (time->Month)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if (time->Day > 31)
        {
            time->Day = 1;
            time->Month++;
        }
        if (time->Month > 12)
        {
            time->Month = 1;
            time->Year++;
        }

        break;

    case 4:
    case 6:
    case 9:
    case 11:
        if (time->Day > 30)
        {
            time->Day = 1;
            time->Month++;
        }

        break;
    case 2:
        if (isLeapYear(time->Year) == 1)
        {
            if (time->Day > 29)
            {
                time->Day = 1;
                time->Month++;
            }
        }
        else
        {
            if (time->Day > 28)
            {
                time->Day = 1;
                time->Month++;
            }
        }
        break;
    }
    if (time->Weekday > 7)
    {
        time->Weekday = 1;
    }
    if (time->Month > 12)
    {
        time->Month = 1;
        time->Year++;
    }
    // Calculate_Weekday(time);
}
void Time_Logic_Sub(Time *time)
{
    if (time->Second > 60)
    {
        time->Minute--;
        time->Second = 59;
    }
    if (time->Minute > 60)
    {
        time->Hour--;
        time->Minute = 59;
    }
    if (time->Hour > 60)
    {
        time->Day--;
        //time->Weekday--;
        time->Hour = 23;
    }
    switch (time->Month)
    {
    case 1:
    case 2:
    case 4:
    case 6:
    case 8:
    case 9:
    case 11:
        if (time->Day == 0)
        {
            time->Day = 31;
            time->Month--;
        }
        if (time->Month == 0)
        {
            time->Month = 12;
            time->Year--;
        }

    case 5:
    case 7:
    case 10:
    case 12:
        if (time->Day == 0)
        {
            time->Day = 30;
            time->Month--;
        }

    case 3:
        if (isLeapYear(time->Year) == 1)
        {
            if (time->Day == 0)
            {
                time->Day = 29;
                time->Month--;
            }
        }
        else
        {
            if (time->Day == 0)
            {
                time->Day = 28;
                time->Month--;
            }
        }
    }
    if (time->Weekday < 1)
    {
        time->Weekday = 7;
    }
    if (time->Month == 0)
    {
        time->Month = 12;
        time->Year--;
    }
    // Calculate_Weekday(time);
}

void Time_Decision(char hang, char lie, Time *times)
{
    if (hang == 1 && lie == 3)
    {
        LCD_ShowNum(1, 3, times->Year, 2);
    }
    if (hang == 1 && lie == 6)
    {
        LCD_ShowNum(1, 6, times->Month, 2);
    }
    if (hang == 1 && lie == 9)
    {
        LCD_ShowNum(1, 9, times->Day, 2);
    }
    // if (hang == 1 && lie == 12)
    // {
    //     LCD_ShowNum(1, 12, times->Weekday, 2);
    // }
    if (hang == 2 && lie == 1)
    {
        LCD_ShowNum(2, 1, times->Hour, 2);
    }
    if (hang == 2 && lie == 4)
    {
        LCD_ShowNum(2, 4, times->Minute, 2);
    }
    if (hang == 2 && lie == 7)
    {
        LCD_ShowNum(2, 7, times->Second, 2);
    }
}

void LCD_ShowALL(Time *times)
{
    LCD_ShowNum(1, 3, times->Year, 2);
    LCD_ShowNum(1, 6, times->Month, 2);
    LCD_ShowNum(1, 9, times->Day, 2);
    LCD_ShowNum(1, 12, times->Weekday, 2);
    LCD_ShowNum(2, 1, times->Hour, 2);
    LCD_ShowNum(2, 4, times->Minute, 2);
    LCD_ShowNum(2, 7, times->Second, 2);
}

void Time_Add(char hang, char lie, Time *times)
{
    if (hang == 1 && lie == 3)
    {
        times->Year++;
        Time_Logic_Add(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(1, 3, times->Year, 2);
    }
    if (hang == 1 && lie == 6)
    {
        times->Month++;
        Time_Logic_Add(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(1, 6, times->Month, 2);
    }
    if (hang == 1 && lie == 9)
    {
        times->Day++;
        //times->Weekday++;
        Time_Logic_Add(times);
        Calculate_Weekday(times);
        // Calculate_Weekday(times);

        LCD_ShowALL(times);
        // LCD_ShowNum(1, 9, times->Day, 2);
    }
    // if (hang == 1 && lie == 12)
    // {
    //     times->Weekday++;
    //     Time_Logic_Add(times);
    //     LCD_ShowALL(times);
    //     //LCD_ShowNum(1, 12, times->Weekday, 2);
    // }
    if (hang == 2 && lie == 1)
    {
        times->Hour++;
        Time_Logic_Add(times);
        Calculate_Weekday(times);

        LCD_ShowALL(times);
        // LCD_ShowNum(2, 1, times->Hour, 2);
    }
    if (hang == 2 && lie == 4)
    {
        times->Minute++;
        Time_Logic_Add(times);
        Calculate_Weekday(times);

        LCD_ShowALL(times);
        // LCD_ShowNum(2, 4, times->Minute, 2);
    }
    if (hang == 2 && lie == 7)
    {
        times->Second++;
        Time_Logic_Add(times);
        Calculate_Weekday(times);

        LCD_ShowALL(times);
        // LCD_ShowNum(2, 7, times->Second, 2);
    }
}

void Time_Sub(char hang, char lie, Time *times)
{
    if (hang == 1 && lie == 3)
    {
        times->Year--;
        LCD_ShowALL(times);
        Calculate_Weekday(times);
        Time_Logic_Sub(times);
        // LCD_ShowNum(1, 3, times->Year, 2);
    }
    if (hang == 1 && lie == 6)
    {
        times->Month--;
        Time_Logic_Sub(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(1, 6, times->Month, 2);
    }
    if (hang == 1 && lie == 9)
    {
        times->Day--;
        //times->Weekday--;
        Time_Logic_Sub(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(1, 9, times->Day, 2);
    }
    // if (hang == 1 && lie == 12)
    // {
    //     times->Weekday--;
    //     Time_Logic_Sub(times);
    //     Calculate_Weekday(struct Time *time);
    //     LCD_ShowALL(times);
    //     //LCD_ShowNum(1, 12, times->Weekday, 2);
    // }
    if (hang == 2 && lie == 1)
    {
        times->Hour--;
        Time_Logic_Sub(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(2, 1, times->Hour, 2);
    }
    if (hang == 2 && lie == 4)
    {
        times->Minute--;
        Time_Logic_Sub(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(2, 4, times->Minute, 2);
    }
    if (hang == 2 && lie == 7)
    {
        times->Second--;
        Time_Logic_Sub(times);
        Calculate_Weekday(times);
        LCD_ShowALL(times);
        // LCD_ShowNum(2, 7, times->Second, 2);
    }
}

unsigned char LCD_Blink(char hang, char lie, Time *times,unsigned char *i)
{
    while (1)
    {
        Key_Change = key();
        switch (num)
        {
        case 0:

        case 2:
            LCD_ShowString(hang, lie, "  ");
            Delay(10);
            break;
        case 1:

        case 3:
            Time_Decision(hang, lie, times);
            Delay(10);
            break;
        }
        if (Key_Change == 3)
        {
            Time_Add(hang, lie, times);
        }
        if (Key_Change == 4)
        {
            Time_Sub(hang, lie, times);
        }
        if (Key_Change == 2)
        {
            *i += 1;
            if (*i > 6)
            {
                *i = 1;
            }
            LCD_ShowNum(2, 10, *i, 2);
            Time_Decision(hang, lie, times);
            break;
        }
        if (Key_Change == 1)
        {

            return 1;
        }
    }
}

void Change_Time(Time *time)
{
    unsigned char i=1;
    unsigned char Out = 1;
    while (1)
    {
        Key_Change = key();
        Timer0Init();
        if (Key_Change == 2)
        {
            i++;
            LCD_ShowNum(2, 10, i, 2);
        }
        switch (i)
        {
        case 1:
            Out = LCD_Blink(1, 3, time,&i);
            break;
        case 2:
            Out = LCD_Blink(1, 6, time,&i);
            break;
        case 3:
            Out = LCD_Blink(1, 9, time,&i);
            break;
            // case 4:
            // LCD_Blink(1, 12, time);

            break;
        case 4:
            Out = LCD_Blink(2, 1, time,&i);
            break;
        case 5:
            Out = LCD_Blink(2, 4, time,&i);
            break;
        case 6:
            Out = LCD_Blink(2, 7, time,&i);
            break;
        }
        if (Out == 1)
        {
            // LCD_ShowNum(2,13,Key_Change,2);
            DS1302_SetTime();
            break;
        }
    }
}

void Timer_Cut() interrupt 1
{

    static unsigned int Count = 0;
    TL0 = 0x66; // 设置定时初值
    TH0 = 0xFC; // 设置定时初值
    Count++;
    if (Count == 400)
    {
        Count = 0;
        num++;
        if (num == 4)
        {
            num = 0;
        }
    }
}